/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shift.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.schedul.entity.SchedulingInformation;
import com.thinkgem.jeesite.modules.shift.dao.ShiftDao;
import com.thinkgem.jeesite.modules.shift.entity.Shift;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;

/**
 * 班次Service
 * 
 * @author hui.li
 * @version 2017-07-22
 */
@Service
@Transactional(readOnly = true)
public class ShiftService extends CrudService<ShiftDao, Shift> {

	public Shift get(String id) {
		return super.get(id);
	}

	public List<Shift> findList(Shift shift) {
		return super.findList(shift);
	}

	public Page<Shift> findPage(Page<Shift> page, Shift shift) {
		Page<Shift> pageNew = super.findPage(page, shift);

		List<Shift> list = pageNew.getList();
		List<Shift> listNew = new ArrayList<Shift>();
		// 查找星期，显示名称
		for (Shift shift2 : list) {
			String str = shift2.getWeek();
			if (null != str && str.length() > 0) {
				int count = str.indexOf(",");
				if (count != -1) {
					String[] array = str.split(",");
					StringBuffer sb = new StringBuffer();
					for (int i = 0; i < array.length; i++) {
						sb.append(DictUtils.getDictLabel(array[i], "week_type",
								""));
						sb.append(",");
					}
					shift2.setSchclassName(sb.substring(0, sb.length()-1));
				} else {
					shift2.setSchclassName(DictUtils.getDictLabel(
							shift2.getWeek(), "week_type", ""));
				}//end if
				listNew.add(shift2);
			}//end if
		}//end for

		pageNew.setList(listNew);
		return pageNew;
	}

	@Transactional(readOnly = false)
	public void save(Shift shift) {
		super.save(shift);
	}

	@Transactional(readOnly = false)
	public void delete(Shift shift) {
		super.delete(shift);
	}

	/**
	 * 根据员工编号，和周几获取班次
	 * @param employeeNumer
	 * @param weekByDate
	 * @return
	 */
	public List<Shift> findAllByEmployeeNumerAndDate(List<SchedulingInformation> schedulingInformationList, int weekByDate) {
		java.util.List<String> ids = new ArrayList<String>();
		for(SchedulingInformation one: schedulingInformationList){
			ids.add(one.getShift().getId());
		}
		return dao.findAllByEmployeeNumerAndDate(StringUtils.join(ids,","), String.valueOf(weekByDate));
	}
}